package com.recycle.recycledemo.recycle;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.widget.Toast;

import com.recycle.recycledemo.R;

import java.util.ArrayList;
import java.util.List;

public class RecycleActivity extends AppCompatActivity {

    private List<String> data = new ArrayList<String>();
    private RecyclerView recyclerView;
    private CommonAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle);
        initData();
        recyclerView = (RecyclerView) findViewById(R.id.recycleView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
//        recyclerView.setLayoutManager(new GridLayoutManager(this,2));
//        adapter = new RecyclerViewAdapter(this,data);
        adapter = new CommonAdapter<String>(this,R.layout.item_recycler,data){
            @Override
            public void convert(CommonViewHolder holder, String s, int position) {
                TextView  tv = holder.getView(R.id.tv);
                tv.setText(s);
            }
        };
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener<String>() {
            @Override
            public void setOnItemClickListener(int Position, String s) {
                Toast.makeText(RecycleActivity.this,"这是第"+Position+"个数据",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void setOnLongItemClickListener(int Position, String s) {

            }
        } );
    }

    /**
     * 生产数据
     */
    private void initData() {
        data.clear();
        for(int i = 0; i <20 ;i++){
            data.add("你是我的小苹果"+i);
        }
    }
}
